<?php
require_once("Artist.php");
require_once("Venue.php");
require_once("LineupItem.php");
require_once("Geocode.php");
require_once("Utility.php");

class Lineup {
    
    public function getEvents($location, $caching = false)
    {
    	$DISTANCE = "";
        $file = "./cache/events/".urlencode(strtolower($location)).".serialized";
        if (@fopen($file, "r") && !$caching) {
            $lineup = unserialize(file_get_contents($file));
            //Info::dump($lineup); die;
            return $lineup;
        }

        $geocode = new Geocode($location);
        $url = "http://ws.audioscrobbler.com/2.0/?method=geo.getevents&long=".$geocode->getLong()."&lat=".$geocode->getLat()."&api_key=519681edb551a3b5177bb67352e16136&distance=".$DISTANCE;
        $sxml = simplexml_load_file($url);
        $totalPages = (int)$sxml->events['totalpages'];
        $totalEvents = (int)$sxml->events['total'];
        if($caching) {
        	echo $location.":\n";
        	echo "Starting caching of ".$totalPages." pages, url: ".$url."\n";
        }
        for($i = 1; $i <= $totalPages; $i++)
        {
            if($caching) {
                echo "xml page ".$i."\n";
            }
            $file2 = "./cache/events/parts/".urlencode(strtolower($location)).".xml.page".$i;
            if(file_exists($file2) && Utility::fileLifetime($file2) < 84600) {
                $xml2 = file_get_contents($file2);
            } else {
                $xml2 = file_get_contents("http://ws.audioscrobbler.com/2.0/?method=geo.getevents&long=".$geocode->getLong()."&lat=".$geocode->getLat()."&api_key=519681edb551a3b5177bb67352e16136&distance=".$DISTANCE."&page=".$i);
            }
            $sxml2 = simplexml_load_string($xml2);
            foreach($sxml2->events->event as $obj) {   
                foreach($obj->artists->artist as $eventArtistName) {
                    $lineupItem = new LineupItem();
                    $lineupItem->venue = new Venue((string)$obj->venue->name);
                    $lineupItem->venue->country = (string)$obj->venue->location->country;
                    $lineupItem->venue->street = (string)$obj->venue->location->street;
                    $lineupItem->venue->postalcode = (string)$obj->venue->postalcode;
                    $lineupItem->startDate = (string)$obj->startDate;
                    $lineupItem->startTime = (string)$obj->startTime;
                    $lineupItem->title = (string)$obj->title;
                    if(!empty($eventArtistName)) {
                        $lineupItem->artist = new Artist((string)$eventArtistName);
                        $lineup[] = $lineupItem;
                    } else {
                        throw new Exception("Lineup:getEvents(): no eventArtistName!");
                    }
                }
            }
            //start caching (and notify user)
            if($i > 1 && !$caching) {
                $c = Cache::getInstance();
                $c->queueEvents($location);
                $out = $c->startCaching(); 
                //echo "lineup caching: ".$out;
                return $lineup;
            }
            //partial caching for locations with many events (countries, areas, large cities)
            if($caching && $totalPages > 9) {
                $fp = @fopen($file2, "w");
                if($fp) {
                    $result = fwrite($fp, $xml2);
                    fclose($fp);
                }
            }
        }
        $fp = @fopen($file, "w");
        if($fp) {
           $result = fwrite($fp, serialize($lineup));
           fclose($fp);
        }
        return $lineup;
    }
}
?>